package com.us.usws.common.resourcetree.impl;

import com.us.usws.common.CommonUtils;
import com.us.usws.common.SiteConstants;
import com.us.usws.common.resourcetree.GenericResourceTreeRawDataProvider;
import com.us.usws.common.resourcetree.model.DefaultResourceTree;

/**
 * default tree provider
 * @author Administrator
 *
 */
public class DefaultTreeProvider extends GenericResourceTreeRawDataProvider {

	private long rootId;
	private String key;

	public DefaultTreeProvider(long rootId) {
		this.rootId = rootId;
	}

	public DefaultTreeProvider(long rootId, String key) {
		this.rootId = rootId;
		this.key = key;
	}

	@Override
	public long getRootId() {
		return rootId;
	}

	@Override
	public String getKey() {
		return key;
	}

	@Override
	public String getSQL() {
		return "select id,parent_id,name,display_name,link_url,icon_cls,is_expand from sys_obj where type_code = '"
				+ SiteConstants.SYSOBJ_TYPE_MENU + "' and id=? order by seq_num";
	}

	@Override
	public DefaultResourceTree processRawData(Object[] row) {
		long id = ((Number) row[0]).longValue();
		long parentId = ((Number) row[1]).longValue();
		String name = (String) row[2];
		String text = (String) row[3];
		String iconCls = (String) row[5];
		boolean expand = CommonUtils.toBooleanDefaultFalse((String) row[6]);

		//gen object
		DefaultResourceTree m = new DefaultResourceTree(id, parentId, text);
		m.setName(name);
		m.setIconCls(iconCls);
		if (id == getRootId()) {
			m.setExpanded(true);
		} else {
			m.setExpanded(expand);
		}

		return m;
	}

	@Override
	public Class<DefaultResourceTree> getNodeClass() {
		return DefaultResourceTree.class;
	}

}
